package com.htkj.demo;

import android.content.Context;
import android.media.AudioManager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shuyu.gsyvideoplayer.utils.GSYVideoType;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer;

public class EmptyControlVideo extends StandardGSYVideoPlayer {
    private static final String TAG = "EmptyControlVideo";
    private ImageView mCoverImage;
    int  mCoverOriginId = 0;
    String  mCoverOriginPath = "";
    private TextView frameRateTextView;
    int mDefaultRes;
    public EmptyControlVideo(Context context, Boolean fullFlag) {
        super(context, fullFlag);
    }

    public EmptyControlVideo(Context context) {
        super(context);
    }

    public EmptyControlVideo(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public int getLayoutId() {
        return R.layout.empty_control_video;
    }

    @Override
    protected void init(Context context) {
        super.init(context);

        mCoverImage = (ImageView) findViewById(R.id.thumbImage);
//        mCoverImage.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        if (mThumbImageViewLayout != null &&
                (mCurrentState == -1 || mCurrentState == CURRENT_STATE_NORMAL || mCurrentState == CURRENT_STATE_ERROR)) {
            mThumbImageViewLayout.setVisibility(VISIBLE);
        }



    }

    @Override
    protected void touchSurfaceMoveFullLogic(float absDeltaX, float absDeltaY) {
        super.touchSurfaceMoveFullLogic(absDeltaX, absDeltaY);
        //不给触摸快进，如果需要，屏蔽下方代码即可
        mChangePosition = false;
        //不给触摸音量，如果需要，屏蔽下方代码即可
        mChangeVolume = false;
        //不给触摸亮度，如果需要，屏蔽下方代码即可
        mBrightness = false;

    }

    @Override
    protected void touchDoubleUp(MotionEvent e) {
//        super.touchDoubleUp(e);
        //不需要双击暂停
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return false;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return false;
    }

    public void loadCoverImageBy(int id, int res) {
        mCoverOriginId = id;
        mDefaultRes = res;
//        mCoverImage.setImageResource(0);
        Glide.with(this).load(id).into(mCoverImage);
//        GlideUtils.loadImageNone(getContext(),id,mCoverImage);
//        mCoverImage.setImageResource(id);
        ;
//        mCoverImage.setImageBitmap(LruBitmapCache.INSTANCE.getBitmapCache(getContext(),id));
//        LruBitmapCache.INSTANCE.getBitmapCache(getContext(), id, new LruBitmapCache.OnLruBitmapCache() {
//            @Override
//            public void onLruBitmap(@Nullable Bitmap bit) {
//                mCoverImage.setImageBitmap(bit);
//            }
//        });
    }
    public void loadCoverImageBy(int id,int tid, int res) {
        mCoverOriginId = id;
        mDefaultRes = res;

//        mCoverImage.setImageResource(0);
//        Glide.with(this).load(id).into(mCoverImage);
//        GlideUtils.loadImageNone(getContext(),id,tid,mCoverImage);
        Glide.with(this).load(id).into(mCoverImage);
//        mCoverImage.setImageResource(id);
    }

    public void loadCoverImageBy(int id) {
        mCoverOriginId = id;
        mCoverImage.setImageResource(id);
    }

    public void loadCoverImageBy(String id) {
        mCoverOriginPath = id;
//        mCoverImage.setImageResource(0);
        Glide.with(this).load(mCoverOriginPath).into(mCoverImage);
//        GlideUtils.loadImageNone(getContext(),id,tid,mCoverImage);
    }




    @Override
    public GSYBaseVideoPlayer startWindowFullscreen(Context context, boolean actionBar, boolean statusBar) {
        GSYBaseVideoPlayer gsyBaseVideoPlayer = super.startWindowFullscreen(context, actionBar, statusBar);
        EmptyControlVideo sampleCoverVideo = (EmptyControlVideo) gsyBaseVideoPlayer;
        if(mCoverOriginId != 0) {
            sampleCoverVideo.loadCoverImageBy(mCoverOriginId, mDefaultRes);
        }
        if(!TextUtils.isEmpty(mCoverOriginPath)){
            sampleCoverVideo.loadCoverImageBy(mCoverOriginPath);
        }
        return gsyBaseVideoPlayer;
    }

    /******************* 下方两个重载方法，在播放开始前不屏蔽封面，不需要可屏蔽 ********************/
    @Override
    public void onSurfaceUpdated(Surface surface) {
        super.onSurfaceUpdated(surface);
        if (mThumbImageViewLayout != null && mThumbImageViewLayout.getVisibility() == VISIBLE) {
            mThumbImageViewLayout.setVisibility(INVISIBLE);
        }
    }
    //
    @Override
    protected void setViewShowState(View view, int visibility) {
        if (view == mThumbImageViewLayout && visibility != VISIBLE) {
            return;
        }
        super.setViewShowState(view, visibility);
    }
    //
    @Override
    public void onSurfaceAvailable(Surface surface) {
        super.onSurfaceAvailable(surface);
        if (GSYVideoType.getRenderType() != GSYVideoType.TEXTURE) {
            if (mThumbImageViewLayout != null && mThumbImageViewLayout.getVisibility() == VISIBLE) {
                mThumbImageViewLayout.setVisibility(INVISIBLE);
            }
        }
    }

    public void setPath(String path){

        setUpLazy(path,false,null,null,"");
    }
    private int streamVolume=50;
    public void startPlay(){
        release();
//        streamVolume=  mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
//        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
        setLooping(true);//视频是否循环播放
        startPlayLogic();
//        addVolume();
    }

    public void stopPlay(){
        onVideoPause();
    }

    private Runnable mVolumeRunble=()->{
           dismissVolumeDialog();
    };

    public void setVoluem(int voluem){
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, voluem, 0);
        int currentMusicVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        Log.e("setVoluem","getVoluemMin()="+getVoluemMin());
        Log.e("setVoluem","getVoluemMax()="+getVoluemMax());
        Log.e("setVoluem","currentMusicVolume()="+currentMusicVolume);
        int pos = (int) ((currentMusicVolume-getVoluemMin()+0f)/(getVoluemMax()-getVoluemMin()+0f)*100);
        Log.e("setVoluem","position="+pos);

        showVolumeDialog(0f,pos);
        getHandler().removeCallbacks(mVolumeRunble);
        getHandler().postDelayed(mVolumeRunble,1000);
    }
    public int getVoluemMax(){
        return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    }
    public int getVoluemMin(){
        int min = 0;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
            min = mAudioManager.getStreamMinVolume(AudioManager.STREAM_MUSIC);
        }
        return min;
    }
    public void addVoluem(){
        int currentMusicVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        if(currentMusicVolume<getVoluemMax()){
            setVoluem(currentMusicVolume+1);
        }
    }
    public void reduceVoluem(){
        int currentMusicVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        if(currentMusicVolume>getVoluemMin()){
            setVoluem(currentMusicVolume-1);
        }
    }

}
